﻿using System;
using System.Collections;
using System.Data;
using CE.Database;

/// <summary>
/// Summary description for CarLoadingEcpDB
/// </summary>
/// 
namespace CE.Provider
{
    public class CarLoadingEcpDB
    {
        public CarLoadingEcpDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

          public Int64 Insert(CarLoadingEcp CarLoadingEcp)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarLoadingEcpInsertUpdateSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@ProviderECPId", CarLoadingEcp.ProviderECPId);
            _sPara.Add("@ProviderCompanyId", CarLoadingEcp.ProviderCompanyId);
            _sPara.Add("@Description", CarLoadingEcp.Description);
            _sPara.Add("@Rate", CarLoadingEcp.Rate);
            _sPara.Add("@RefCEUserId", CarLoadingEcp.RefCEUserId);
            _sPara.Add("@Status", CarLoadingEcp.Status);
            _sPara.Add("@Finfo1", CarLoadingEcp.FInfo1);
            _sPara.Add("@Dinfo1", CarLoadingEcp.DInfo1);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public CarLoadingEcpList GetProviderCarLoadingEcpList(Int64 ProviderCompanyId)
        {
            CarLoadingEcpList _CarLoadingEcplist = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarLoadingEcpSelectItemList";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _CarLoadingEcplist = new CarLoadingEcpList();
                    for (int i = 0; i < _dt.Rows.Count; i++)
                    {
                        CarLoadingEcp _CarLoadingEcp = new CarLoadingEcp();
                        _CarLoadingEcp = FillDataRecord(_dt, i);
                        _CarLoadingEcplist.Add(_CarLoadingEcp);
                    }
                }
            }
            return _CarLoadingEcplist;
        }


        public CarLoadingEcp GetProviderCarLoadingEcp(Int64 ProviderCompanyId, Int32 ProviderECPId)
        {
            CarLoadingEcp _vehicleyear = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarLoadingEcpSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);
            _spara.Add("@ProviderECPId", ProviderECPId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _vehicleyear = new CarLoadingEcp();
                    _vehicleyear = FillDataRecord(_dt, 0);
                }
            }
            return _vehicleyear;
        }

        public CarLoadingEcp GetProviderCarLoadingEcp(Int32 ProviderECPId)
        {
            CarLoadingEcp _vehicleyear = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarLoadingEcpSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderECPId", ProviderECPId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _vehicleyear = new CarLoadingEcp();
                    _vehicleyear = FillDataRecord(_dt, 0);
                }
            }
            return _vehicleyear;
        }

        private CarLoadingEcp FillDataRecord(DataTable myDataTable, int RowId)
        {
            CarLoadingEcp myCarLoadingEcp = new CarLoadingEcp();
            myCarLoadingEcp.ProviderECPId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderECPId"].ToString());
            myCarLoadingEcp.ProviderCompanyId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCompanyId"].ToString());
            myCarLoadingEcp.Description = myDataTable.Rows[RowId]["Description"].ToString();
            myCarLoadingEcp.Rate = Convert.ToSingle(myDataTable.Rows[RowId]["Rate"].ToString());
            myCarLoadingEcp.RefCEUserId = Convert.ToInt64(myDataTable.Rows[RowId]["RefCEUserId"].ToString());
            myCarLoadingEcp.Status = Convert.ToBoolean(myDataTable.Rows[RowId]["Status"].ToString());
            myCarLoadingEcp.ModifiedDate = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());

            if (myDataTable.Rows[RowId]["FInfo1"].ToString() != "")
                myCarLoadingEcp.FInfo1 = Convert.ToSingle(myDataTable.Rows[RowId]["FInfo1"].ToString());

            if (myDataTable.Rows[RowId]["DInfo1"].ToString() != "")
                myCarLoadingEcp.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["DInfo1"].ToString());
            else
                myCarLoadingEcp.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());

            return myCarLoadingEcp;
        }
    }

}